package com.tianfei.crowd.controller;

import com.tianfei.crowd.api.MySQLRemoteService;
import com.tianfei.crowd.constant.CrowdConstants;
import com.tianfei.crowd.entity.vo.AddressVO;
import com.tianfei.crowd.entity.vo.MemberLoginVO;
import com.tianfei.crowd.entity.vo.OrderProjectVO;
import com.tianfei.crowd.utils.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * 订单 微服务端的 本地 controller
 *
 * @author: Herz
 * @date: 2021/8/9 19:22
 */
@Controller
public class OrderConsumerController {

    @Autowired
    private MySQLRemoteService mySQLRemoteService;


    @RequestMapping("/save/address")
    public String saveAddress(AddressVO addressVO, HttpSession session){

        // 1、从session域 中获取合并后的 回报数量
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");

        Integer returnCount = orderProjectVO.getReturnCount();

        // 2、执行保存
        ResultEntity<String> resultEntity = mySQLRemoteService.saveAddressVORemote(addressVO);

        // 3、重定向到确认订单页面
        return "redirect:http://localhost/order/confirm/order/" + returnCount;
    }


    @RequestMapping("/confirm/order/{returnCount}")
    public String showConfirmOrder(
            @PathVariable("returnCount") Integer returnCount,
            HttpSession session){

        // 1、把当前接受到的回报数量合并到 Session 域
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");

        orderProjectVO.setReturnCount(returnCount);

        session.setAttribute("orderProjectVO", orderProjectVO);


        // 2、获取当前已登录用户的id
        MemberLoginVO memberLoginVO = (MemberLoginVO) session.getAttribute(CrowdConstants.MESSAGE_ATTR_MEMBER);

        Integer memberId = memberLoginVO.getId();

        // 3、查询目前的收货地址数据
        ResultEntity<List<AddressVO>> resultEntity = mySQLRemoteService.getAddressVORemote(memberId);

        if (ResultEntity.SUCCESS.equals(resultEntity.getResult())){
            List<AddressVO> addressVOList = resultEntity.getData();

            session.setAttribute("addressVOList", addressVOList);
        }

        return "confirm-order";
    }


    @RequestMapping("/confirm/return/info/{returnId}")
    public String showReturnInfo(
            @PathVariable("returnId") Integer returnId,
            HttpSession session){

        ResultEntity<OrderProjectVO> resultEntity = mySQLRemoteService.getOrderProjectVORemote(returnId);

        if (ResultEntity.SUCCESS.equals(resultEntity.getResult())){

            OrderProjectVO orderProjectVO = resultEntity.getData();

            session.setAttribute("orderProjectVO", orderProjectVO);
        }

        return "confirm-return";
    }

}
